package com.study.servlet.request;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.study.servlet.domain.User;

public class SetRequestParam2JavaBean extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			setRequestParams2JavaBeanByBeanUtils(request, response) ;
		} catch (Exception e) {
			e.printStackTrace() ;
		}
	}
	
	/**
	* @Name: printAllRequestParamsInfo
	* @Description: 打印页面表单提交的所有参数信息
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-3-1 下午6:15:42
	* @Parameters: @param request
	* @Parameters: @param response
	* @Parameters: @throws ServletException
	* @Parameters: @throws IOException
	* @Return: void
	 */
	private void printAllRequestParamsInfo(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8") ;
		PrintWriter out = response.getWriter() ;
		Enumeration<String> parameterNames = request.getParameterNames() ;
		while(parameterNames.hasMoreElements()) {
			String parameterName = parameterNames.nextElement() ;
			String[] parameterValues = request.getParameterValues(parameterName) ;
			out.print(parameterName + ":" + Arrays.toString(parameterValues) + "<br/>") ;
		}
		out.flush() ;
		out.close() ;
	}
	
	/**
	* @Name: setRequestParams2JavaBeanByBeanUtils
	* @Description: 使用BeanUtils插件,实现将页面请求参数封装到表单JavaBean
	* @Author: 李超	lc_jack@foxmail.com
	* @Version: V1.0
	* @CreateDate: 2017-3-1 下午6:29:24
	* @Parameters: @param request
	* @Parameters: @param response
	* @Parameters: @throws ServletException
	* @Parameters: @throws IOException
	* @Parameters: @throws Exception
	* @Return: void
	 */
	private void setRequestParams2JavaBeanByBeanUtils(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException, Exception {
		response.setContentType("text/html;charset=UTF-8") ;
		PrintWriter out = response.getWriter() ;
		User user = new User() ;
		Map<String, Object> parameterMap = request.getParameterMap() ;
		//使用BeanUtils.copyProperty(bean, name, value)
//		for(Map.Entry<String, Object> entry : parameterMap.entrySet()) {
//			String key = entry.getKey() ;
//			Object value = entry.getValue() ;
//			BeanUtils.copyProperty(user, key, value) ;
//		}
		//使用BeanUtils.populate(bean, properties)
		BeanUtils.populate(user, parameterMap) ;
		out.print(user) ;
		out.flush() ;
		out.close() ;
	}
	
	
	

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

}
